package com.hzxy.yiyid.web.module.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzxy.yiyid.web.common.base.BaseResult;
import com.hzxy.yiyid.web.module.controller.vo.FoodCommandVo;
import com.hzxy.yiyid.web.module.repository.FoodTypeMapper;
import com.hzxy.yiyid.web.module.repository.entity.FoodRecommend;
import com.hzxy.yiyid.web.module.repository.entity.FoodType;
import com.hzxy.yiyid.web.module.service.FoodTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FoodTypeServicempl extends ServiceImpl<FoodTypeMapper, FoodType> implements FoodTypeService {

    @Autowired
    private FoodTypeMapper foodTypeMapper;
    @Override
    public BaseResult<?> foodRechange(Integer foodTypeId) {

        FoodCommandVo foodRecommend = new FoodCommandVo();
        //通过食物类型拿到类型id
        List<FoodRecommend> food = foodTypeMapper.getFoodIdByFoodtype(foodTypeId);
        int sumFoodCalorie = 0;
        List<Integer> collect = food.stream()
                .map(FoodRecommend::getFoodCalorie)
                .collect(Collectors.toList());

        for (Integer calorie : collect) {
            sumFoodCalorie+=calorie;
        }
        foodRecommend.setMaxCalorie(sumFoodCalorie+250);
        foodRecommend.setMinCalorie(sumFoodCalorie-50);
        foodRecommend.setCalorie(sumFoodCalorie);
        foodRecommend.setFoodRecommends(food);
        //第一次将所有符合食物类型id的前三个返回
        return BaseResult.success(foodRecommend);
    }

    @Override
    public BaseResult<?> foodRechangeById(Integer btnid, Integer foodTypeId) {

        FoodCommandVo foodRecommend = new FoodCommandVo();
        if (btnid == 1){
            List<FoodRecommend> food = foodTypeMapper.getFoodByBtnidThree(foodTypeId);
            int sumFoodCalorie = 0;
            List<Integer> collect = food.stream()
                    .map(FoodRecommend::getFoodCalorie)
                    .collect(Collectors.toList());

            for (Integer calorie : collect) {
                sumFoodCalorie+=calorie;
            }
            foodRecommend.setMaxCalorie(sumFoodCalorie+250);
            foodRecommend.setMinCalorie(sumFoodCalorie-50);
            foodRecommend.setCalorie(sumFoodCalorie);
            foodRecommend.setFoodRecommends(food);
            return BaseResult.success(foodRecommend);
        }else if(btnid == 2){
            List<FoodRecommend> food = foodTypeMapper.getFoodByBtnidSix(foodTypeId);
            int sumFoodCalorie = 0;
            List<Integer> collect = food.stream()
                    .map(FoodRecommend::getFoodCalorie)
                    .collect(Collectors.toList());

            for (Integer calorie : collect) {
                sumFoodCalorie+=calorie;
            }
            foodRecommend.setMaxCalorie(sumFoodCalorie+250);
            foodRecommend.setMinCalorie(sumFoodCalorie-50);
            foodRecommend.setCalorie(sumFoodCalorie);
            foodRecommend.setFoodRecommends(food);
            return BaseResult.success(foodRecommend);
        }else{
            List<FoodRecommend> food = foodTypeMapper.getFoodByBtnidZero(foodTypeId);
            int sumFoodCalorie = 0;
            List<Integer> collect = food.stream()
                    .map(FoodRecommend::getFoodCalorie)
                    .collect(Collectors.toList());

            for (Integer calorie : collect) {
                sumFoodCalorie+=calorie;
            }
            foodRecommend.setMaxCalorie(sumFoodCalorie+250);
            foodRecommend.setMinCalorie(sumFoodCalorie-50);
            foodRecommend.setCalorie(sumFoodCalorie);
            foodRecommend.setFoodRecommends(food);
            return BaseResult.success(foodRecommend);
        }
    }
}
